﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class cms_ProductsComments : System.Web.UI.Page
{
    #region CONSTANTS

    private const string PRODUCT_ID = "ProductIDForComments";

    #endregion CONSTANTS

    #region VARIABLES

    public Products product;
    int IDRow = 0;

    #endregion VARIABLES

    #region METHODS

    private void ProductsBind()
    {
        gvProducts.DataSource = ProductsManager.Instance.GetProductsSortedByCommentsDate(CommonTools.CurrentLanguageID);
        gvProducts.DataBind();
    }

    private void CommentsBind(int ProductID)
    {
        product = ProductsManager.Instance.GetProduct(ProductID, CommonTools.CurrentLanguageID);
        gvComments.DataSource = ProductsManager.Instance.GetCommentForProduct(ProductID, true);
        gvComments.DataBind();
        lProduct.Text = "  - " + (product.Name.Length > 40 ? product.Name.Substring(0, 40) + "..." : product.Name);
    }

    private string GetIDsToAccept()
    {
        string result = string.Empty;
        bool AcceptChecked;
        foreach (GridViewRow row in gvComments.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                AcceptChecked = ((CheckBox)row.FindControl("cbAccept")).Checked;
                if (AcceptChecked)
                {
                    result += row.Cells[IDRow].Text + ",";
                }
            }
        }
        return result.TrimEnd(',');
    }

    private string GetIDsToUnaccept()
    {
        string result = string.Empty;
        bool AcceptChecked;
        foreach (GridViewRow row in gvComments.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                AcceptChecked = ((CheckBox)row.FindControl("cbAccept")).Checked;
                if (!AcceptChecked)
                {
                    result += row.Cells[IDRow].Text + ",";
                }
            }
        }
        return result.TrimEnd(',');
    }

    private string GetIDsToHide()
    {
        string result = string.Empty;
        bool HideChecked;
        foreach (GridViewRow row in gvComments.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                HideChecked = ((CheckBox)row.FindControl("cbHide")).Checked;
                if (HideChecked)
                {
                    result += row.Cells[IDRow].Text + ",";
                }
            }
        }
        return result.TrimEnd(',');
    }

    private string GetIDsToUnhide()
    {
        string result = string.Empty;
        bool HideChecked;
        foreach (GridViewRow row in gvComments.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                HideChecked = ((CheckBox)row.FindControl("cbHide")).Checked;
                if (!HideChecked)
                {
                    result += row.Cells[IDRow].Text + ",";
                }
            }
        }
        return result.TrimEnd(',');
    }

    #endregion METHODS

    #region EVENTS

    protected void ShowComments(object sender, CommandEventArgs e)
    {
        Session[PRODUCT_ID] = Convert.ToInt32(e.CommandArgument);
        CommentsBind(Convert.ToInt32(e.CommandArgument));

        pnlProducts.Visible = false;
        pnlComments.Visible = true;
    }

    protected void CommentsCommand(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Save":
                {
                    if (ProductsManager.Instance.UpdateCommentsState((int)Session[PRODUCT_ID], GetIDsToAccept(), GetIDsToUnaccept(), GetIDsToHide(), GetIDsToUnhide()))
                        ctrlShowInfo.ShowInfo(false, Resources.Language.ChangesSaved);
                    else
                        ctrlShowInfo.ShowInfo(true, Resources.Language.cmsMessageDataSaveFailure);
                }
                break;
            case "Cancel":
                CommentsBind((int)Session[PRODUCT_ID]);
                break;
            case "Back":
                pnlComments.Visible = false;
                pnlProducts.Visible = true;
                ProductsBind();
                break;
        }
    }

    protected void gvComments_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvComments.PageIndex = e.NewPageIndex;
        CommentsBind((int)Session[PRODUCT_ID]);
    }

    protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProducts.PageIndex = e.NewPageIndex;
        ProductsBind();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            ProductsBind();
    }

    #endregion PAGE EVENTS
}
